package com.example.sxproject.mapper;

import com.example.sxproject.pojo.FinancialUser;
import com.example.sxproject.pojo.LoanProduct;
import com.example.sxproject.pojo.LoanUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface LoanMapper {

    //查询select：查询全部
    @Select("select * from loan_product")
    List<LoanProduct> SelectAll();

    //查询select：根据产品id查找
    @Select("select * from loan_product where id = #{productId}")
    LoanProduct SelectByProductId(Integer productId);

    //查询select：根据用户名查找 用户-贷款表
    @Select("SELECT p.* FROM loan_user u JOIN loan_product p ON u.belongProduct = p.id WHERE u.belongUser = #{userId};")
    List<LoanProduct> SelectByUserId(Integer userId);

    //查询select：根据产品id查找
    @Select("select * from loan_user where userId = #{userId}")
    List<LoanUser> findByUserId(Integer userId);

    //添加insert：添加一个产品
    @Insert("INSERT INTO loan_product( productName, beginDate, timeContinue, incomeType, beginMoney, loanRate, interest, loanStatus) " +
            "VALUES (#{productName}, #{beginDate}, #{timeContinue}, #{incomeType}, #{beginMoney}, #{loanRate}, #{interest}, '1')")
    void addLoanProduct(LoanProduct loanProduct);

    //添加insert：添加一个用户-产品
    @Insert("INSERT INTO loan_user (id, userId, productId, balance, interest, loanRate, endDate, productName, productStatus) " +
            "VALUES (#{id}, #{userId}, #{productId}, #{balance}, #{interest}, #{loanRate}, #{endDate}, #{productName}, #{productStatus})")
    void addUserAndLoan(LoanUser loanUser);

    //更新操作：更新贷款
    @Update("UPDATE loan_product SET productName = #{productName}, beginDate = #{beginDate}, " +
            "timeContinue = #{timeContinue}, incomeType = #{incomeType}, beginMoney = #{beginMoney}, " +
            "loanRate = #{loanRate}, interest = #{interest} WHERE id = #{id}")
    void update(LoanProduct loanProduct);

    //更新操作：更新用户-贷款
    @Update("UPDATE loan_user SET belongUser = #{belongUser}, belongProduct = #{belongProduct}," +
            " balance = #{balance}, productStatus = #{productStatus} " +
            "WHERE id = #{id}")
    void updateUP(LoanUser loanUser);

    //更新update：实现假删除
    @Update("update loan_product set loanStatus = #{loanStatus} " +
            "where id = #{id}")
    void delete(LoanProduct loanProduct);

    //管理员贷款产品查询
    @Select({
            "<script>",
            "SELECT *",
            "FROM loan_product",
            "<where>",
            "<if test='productName!= null'>",
            "productName=#{productName}",
            "</if>",
            "<if test='incomeType!= null'>",
            "AND incomeType = #{incomeType}",
            "</if>",
            "</where>",
            "</script>",
    })
    List<LoanProduct> selectNT(LoanProduct loanProduct);

    @Select("select * from loan_user where productStatus = '1'")
    List<LoanUser> selectApply();

    @Select("select * from loan_user ")
    List<LoanUser> selectAllUser();

    //管理员通过贷款申请
    @Update("update loan_user set productStatus = '2' where id = #{id}")
    void tongguo(Integer id);

    //管理员拒绝贷款申请
    @Update("update loan_user set productStatus = '4' where id = #{id}")
    void jujue(Integer id);
}
